package cn.bjxy.javaks.store_ms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bjxy.javaks.store_ms.entity.GoodsEntity;
import cn.bjxy.javaks.store_ms.utils.DateConvertUtil;
import cn.bjxy.javaks.store_ms.utils.JdbcUtil;

public class GoodsDaoImpl implements IGoodsDao {

	private Connection con;
	private PreparedStatement pStat;
	private ResultSet rs;
	
	@Override
	public List<GoodsEntity> findAll() {
		try {
			String sql = "SELECT * FROM goods_info";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			rs = pStat.executeQuery();
			
			ArrayList<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
			//遍历结果集
			while(rs.next()) {
				GoodsEntity goods = new GoodsEntity();   //封装查询到的管理员信息
				goods.setId(rs.getString("id"));
				goods.setName(rs.getString("name"));
				goods.setAll_num(rs.getInt("all_num"));
				goods.setCurrent_num(rs.getInt("current_num"));
				goods.setBuyer_name(rs.getString("buyer_name"));
				goods.setDescribe(rs.getString("describe"));
				goods.setBuy_date(rs.getTimestamp("buy_date"));
				goods.setExpire_date(rs.getTimestamp("expire_date"));
				goods.setAdd_date(rs.getTimestamp("add_date"));
				goods.setModify_date(rs.getTimestamp("modify_date"));
				goodsList.add(goods);
			}
			return goodsList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public GoodsEntity findById(String id) {
		try {
			String sql = "SELECT * FROM goods_info WHERE id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, id);
			rs = pStat.executeQuery();
			
			GoodsEntity goods = null;   //封装查询到的管理员信息
			//遍历结果集
			if(rs.next()) {
				goods = new GoodsEntity();
				goods.setId(rs.getString("id"));
				goods.setName(rs.getString("name"));
				goods.setAll_num(rs.getInt("all_num"));
				goods.setCurrent_num(rs.getInt("current_num"));
				goods.setBuyer_name(rs.getString("buyer_name"));
				goods.setDescribe(rs.getString("describe"));
				goods.setBuy_date(rs.getTimestamp("buy_date"));
				goods.setExpire_date(rs.getTimestamp("expire_date"));
				goods.setAdd_date(rs.getTimestamp("add_date"));
				goods.setModify_date(rs.getTimestamp("modify_date"));
			}
			return goods;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public List<GoodsEntity> findByName(String name) {
		try {
			String sql = "SELECT * FROM goods_info WHERE name=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, name);
			rs = pStat.executeQuery();
			
			ArrayList<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
			//遍历结果集
			while(rs.next()) {
				GoodsEntity goods = new GoodsEntity();   //封装查询到的管理员信息
				goods.setId(rs.getString("id"));
				goods.setName(rs.getString("name"));
				goods.setAll_num(rs.getInt("all_num"));
				goods.setCurrent_num(rs.getInt("current_num"));
				goods.setBuyer_name(rs.getString("buyer_name"));
				goods.setDescribe(rs.getString("describe"));
				goods.setBuy_date(rs.getTimestamp("buy_date"));
				goods.setExpire_date(rs.getTimestamp("expire_date"));
				goods.setAdd_date(rs.getTimestamp("add_date"));
				goods.setModify_date(rs.getTimestamp("modify_date"));
				goodsList.add(goods);
			}
			return goodsList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int delete(String id) {
		try {
			String sql = "delete from goods_info where id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1,id);
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int add(GoodsEntity goods) {
		try {
			String sql = "INSERT INTO goods_info VALUES(?,?,?,?,?,?,?,?,?,?);";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1,goods.getId());
			pStat.setString(2, goods.getName());
			pStat.setInt(3, goods.getAll_num());
			pStat.setInt(4, goods.getCurrent_num());
			pStat.setString(5, goods.getBuyer_name());
			pStat.setString(6, goods.getDescribe());
			pStat.setTimestamp(7, DateConvertUtil.dateToTimeStamp(goods.getBuy_date()));
			pStat.setTimestamp(8, DateConvertUtil.dateToTimeStamp(goods.getExpire_date()));
			pStat.setTimestamp(9, new Timestamp(System.currentTimeMillis()));
			pStat.setTimestamp(10, new Timestamp(new Date().getTime()));
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int modify(GoodsEntity goods) {
		try {
			String sql = "UPDATE goods_info SET NAME=?, all_num=?,current_num=?, buyer_name=?,`describe`=?,buy_date=?,expire_date=? WHERE id=?;";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1, goods.getName());
			pStat.setInt(2, goods.getAll_num());
			pStat.setInt(3, goods.getCurrent_num());
			pStat.setString(4, goods.getBuyer_name());
			pStat.setString(5, goods.getDescribe());
			pStat.setTimestamp(6, DateConvertUtil.dateToTimeStamp(goods.getBuy_date()));
			pStat.setTimestamp(7, DateConvertUtil.dateToTimeStamp(goods.getExpire_date()));
			pStat.setString(8,goods.getId());
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}
}
